import fs from 'fs-extra'
import type {Manifest} from 'webextension-polyfill'
import type PkgType from '../package.json'
import {isDev, port, r} from '../scripts/utils'

export async function getManifest() {
    const pkg = await fs.readJSON(r('package.json')) as typeof PkgType

    // update this file to update this manifest.json
    // can also be conditional based on your need
    const manifest: Manifest.WebExtensionManifest = {
        manifest_version: 2,
        name: pkg.displayName || pkg.name,
        version: pkg.version,
        description: pkg.description,
        // action: {
        //   default_icon: './assets/icon.png',
        //   default_popup: './dist/popup/index.html',
        // },
        browser_action: {
            default_icon: './assets/icon.png',
            default_popup: './dist/popup/index.html',
        },
        // options_ui: {
        //   default_icon: './assets/icon.png',
        //   default_popup: './dist/popup/index.html',
        // },
        options_ui: {
            page: './dist/options/index.html',
            // open_in_tab: true,
        },
        background: {
            // service_worker: './dist/background/index.mjs',
            scripts: ['./dist/background/index.mjs'],
        },
        icons: {
            16: './assets/icon.png',
            48: './assets/icon.png',
            128: './assets/icon.png',
        },
        permissions: [
            '*://*.tmall.com/',
            '*://*.tmall.com/',
            '*://*.tmall.hk/',
            '*://*.taobao.com/',
            '*://*.tmall.com/*',
            '*://*.taobao.com/*',
            '*://one.alimama.com/*',
            'tabs',
            'activeTab',
            'storage',
            'cookies',
            'management',
            'webRequest',
            'contextMenus',
            'notifications',
            'unlimitedStorage',
            'webRequestBlocking',
        ],
        devtools_page: './devtool.html',
        content_scripts: [
            {
                matches: [
                    // '*://s.taobao.com/search*',
                    // '*://item.taobao.com/item.htm*',
                    // '*://detail.tmall.com/item.htm*',
                    // '*://chaoshi.detail.tmall.com/item.htm*',
                    // '*://pages.tmall.com/*',
                    '*://*.tmall.com/',
                    '*://*.taobao.com/',
                    '*://*.tmall.com/*',
                    '*://*.taobao.com/*',
                    '*://*.tmall.hk/*',
                    '*://one.alimama.com/*',
                    '*://*.yangkeduo.com/*',
                    '*://*.jd.com/*',
                ],
                all_frames: true,
                js: [
                    'dist/contentScripts/index.global.js',
                ],
                css: [
                    'dist/contentScripts/style.css',
                ],
                run_at: "document_start"
            },
        ],
        // web_accessible_resources: [
        //   {
        //     resources: ['dist/contentScripts/style.css'],
        //     matches: ['<all_urls>'],
        //   },
        // ],
        web_accessible_resources: ['*'],

        content_security_policy: 'script-src \'self\' \'unsafe-eval\'; object-src \'self\'',

        // pupup
        // content_security_policy: {
        //   extension_pages: isDev
        //   // this is required on dev for Vite script to load
        //     ? `script-src \'self\' http://localhost:${port}; object-src \'self\'`
        //     : 'script-src \'self\'; object-src \'self\'',
        // },
        // content_security_policy: {
        //   extension_pages: 'script-src \'self\' \'unsafe-eval\'; object-src \'self\'',
        // },
    }

    // FIXME: not work in MV3
    if (isDev) {
      // for content script, as browsers will cache them for each reload,
      // we use a background script to always inject the latest version
      // see src/background/contentScriptHMR.ts
      // delete manifest.content_scripts
      manifest.permissions?.push('webNavigation')
      // this is required on dev for Vite script to load
      manifest.content_security_policy = `script-src \'self\' http://localhost:${port}; object-src \'self\'`
    }

    return manifest
}
